package org.groupg.practice.guava;

import com.google.common.util.concurrent.RateLimiter;

public class RateLimiterExample {
    public static void main(String[] args) {
        RateLimiter rateLimiter = RateLimiter.create(2.0); // 每秒两个许可
        for (int i = 0; i < 10; i++) {
            new Thread(new Worker(rateLimiter)).start();
        }
    }

    static class Worker implements Runnable {
        private final RateLimiter rateLimiter;

        Worker(RateLimiter rateLimiter) {
            this.rateLimiter = rateLimiter;
        }

        @Override
        public void run() {
            rateLimiter.acquire();
            System.out.println(Thread.currentThread().getName() + " 正在执行");
        }
    }
}